from django.test import TestCase

# Create your tests here.
import time
# print(time.localtime().tm_year)
import redis
# r = redis.Redis(host='192.168.137.10',port=6379,decode_responses=True,db=1)  # 默认连本地
# # r.set('name','liuhaizhang')
# r.set('student31200211','232',ex=3)
def get_redis():
    from studentSystemt.utils.redis_con import POOL
    import redis
    r = redis.Redis(connection_pool=POOL,decode_responses=True)
    return r
# r = get_redis()
# # print(r.get('name1'))
# # print(r.hmget('dic1',['name','age']))
# r.lpush('lis1',1,2,3,4,5)
# print(r.lrange('lis1',0,-1))
from io import StringIO
from studentSystemt.apps.user import models
from django.http import HttpResponse
from xlwt import *
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
class Export:
    def excel_export(self):
        """
        导出excel表格
        """
        list_obj = models.User.objects.filter(stuclass='200732')
        if list_obj:
            # 创建工作薄
            ws = Workbook(encoding='utf-8')
            w = ws.add_sheet(u"数据报表第一页")
            w.write(0, 0, "id")
            w.write(0, 1, u"姓名")
            w.write(0, 2, u"学号")
            w.write(0, 3, u"手机")
            w.write(0, 4, u"班级")
            # 写入数据
            excel_row = 1
            for obj in list_obj:
                w.write(excel_row, 0, obj.id)
                w.write(excel_row, 1, obj.name)
                w.write(excel_row, 2, obj.username)
                w.write(excel_row, 3, obj.telephone)
                w.write(excel_row, 4, obj.stuclass)
                excel_row += 1
            # # 检测文件是够存在
            # # 方框中代码是保存本地文件使用，如不需要请删除该代码
            # ###########################

            # exist_file = os.path.exists("test.xls")
            # if exist_file:
            #     os.remove(r"test.xls")
            # ws.save("test.xls")
            # ############################
            # sio = StringIO.StringIO()
            # ws.save(sio)
            # sio.seek(0)
            # response = HttpResponse(sio.getvalue(), content_type='application/vnd.ms-students_excels')
            # response['Content-Disposition'] = 'attachment; filename=test.xls'
            # response.write(sio.getvalue())
            path = os.path.join(BASE_DIR, 'studentSystemt', 'studentSystemt', 'media', 'students_excels','200732.xlsx')
            ws.save(path)
            return

print(BASE_DIR)